<?php

require_once dirname(__FILE__) . '/../lib/experienceGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/experienceGeneratorHelper.class.php';

/**
 * experience actions.
 *
 * @package    docplus.co.za
 * @subpackage experience
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class experienceActions extends autoExperienceActions
{

    /**
     * Executes share action
     *
     * @param sfRequest $request A request object
     */
    public function executeShare(sfWebRequest $request)
    {
        $this->form = new ExperienceForm();
        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    // send the DocPlus team this experience sharing for moderation
                    $experience = $this->form->save();
                    $this->getUser()->setFlash('notice', 'Thanks for sharing your experience with us, will moderate it first and then make it public!');

                    $this->redirect("/share-experience");
                }
                catch (Exception $e)
                {
                    die($e);
                    // invoke the event here so we get the error via email
                }
            }
            else
            {
//                foreach ($this->form as $field)
//                {
//                    echo $field->renderLabel() . " " . $field->renderError() . "<br />";
//                }
//                die($this->form->renderGlobalErrors());
            }
        }
    }

    /**
     * Executes rate action
     *
     * @param sfRequest $request A request object
     */
    public function executeRate(sfWebRequest $request)
    {
        if ($request->hasParameter("experience_id") && $request->hasParameter("rating_value"))
        {
            ExperienceRatingTable::getInstance()->updateOrCreateByExperienceIdAndRatingValue($request->getParameter("experience_id"), $request->getParameter("rating_value"));
            $this->experience = ExperienceTable::getInstance()->findOneById($request->getParameter("experience_id"));
        }
    }

}
